package com.mengfou.appserver;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.mengfou.appserver.app.AppService;
import com.mengfou.appserver.app.IpAddressUtil;
import com.mengfou.appserver.app.PermissionUtil;

public class MainActivity extends AppCompatActivity {

    private static final int FOREGROUND_SERVICE_CODE = 1;
    private static final String TAG = MainActivity.class.getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startBackgroundService();

        TextView textView = (TextView) findViewById(R.id.text_info);

        String ipAddress = IpAddressUtil.getIpAddress(this);
        if(ipAddress == null) {
            textView.setText("网络未连接");
        } else {
            textView.setText(ipAddress);
        }
    }

    private void checkForegroundServicePermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            if (PermissionUtil.hasPermission(this, Manifest.permission.FOREGROUND_SERVICE)) {
                PermissionUtil.requestPermission(this, Manifest.permission.FOREGROUND_SERVICE, FOREGROUND_SERVICE_CODE);
            }
        }
    }

    private void startBackgroundService() {
        checkForegroundServicePermission();
        Intent intent = new Intent(this, AppService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(intent);
        } else {
            startService(intent);
        }
    }


}